{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Project: 1-D Finite Difference Method\n",
    "\n",
    "The purpose of this project is to introduce the finite difference method for solving differential equations in 1-D. Consider 1-D Poisson equation in $(0,1)$ with Dirichlet boundary condition\n",
    "\n",
    "$$ - u'' = f \\,\\; {\\rm in }\\, (0,1), \\quad u(0) = a, u(1) = b. $$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 1: Generate a grid\n",
    "\n",
    "Generate a vector representing a uniform grid with size `h` of (0,1)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAGkCAIAAACgjIjwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA\nB3RJTUUH4QgTBDQBqDS6igAAACR0RVh0U29mdHdhcmUATUFUTEFCLCBUaGUgTWF0aFdvcmtzLCBJ\nbmMuPFjdGAAAACJ0RVh0Q3JlYXRpb24gVGltZQAxOS1BdWctMjAxNyAxMjo1MjowMOsuN7cAACAA\nSURBVHic7d1/VFR1/sfxz8jIEIU5p6YGbJM8xybLE+mIo0O0zFRuxwPZ7jkd6Y/KzTqxxhzXjbSE\nWu3U1w50+grYtss55ddaiX4ejxyz9Vu6YO52WMfOqTgrTMaQLIGwOEDCsPy43z9mm2aV0K/OzH2D\nz8fpj/uJt3NfwXhfc3+kBk3TFAAAepumdwAAAJSikAAAQlBIAAARKCQAgAgUEgBABAoJACAChQQA\nEIFCAgCIQCEBAESgkAAAIlBIAAARKCQAgAgUEgBABAoJACAChQQAEIFCAgCIQCEBAESgkAAAIlBI\nAAARKCQAgAgUEgBABAoJACAChQQAEIFCAgCIQCEBAESY3IX03XffHT9+XO8UAIAomNyFtG3btpdf\nflnvFACAKJishVReXn7fffdt375d7yAAgOgw6h3gPDmdTrvdvmfPHk3T9M4CAIiCyVpImZmZSqkv\nv/zS7/frnQUAEAWT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aNHnU7n7NmzlVJpaWnXXntta2trPAP0\n9vY+9NBDV199tVLKZrMlJCSMjo5GN8BZM4TU1taazWa73R71vZ9LgKNHj95www1tbW0xuk41cYAj\nR44YDIa8vDxN05KSkvbt2xeL78M5/gYcHR0tKCj4zW9+c91118UzgMFgMBqNobfijBkzpk+fnpiY\nGN0AZ83g9XqXLl165ZVXKqXmzJlz8803Hzx4MOoZThOHo1BUTLVC6uzsTE1NDS+tVuuJEyf03WNm\nZuatt94a6gNdMuTl5b388suhbb/ff+zYsYyMjHgGWLhwYUFBwXfffff+++8/+uijDzzwQFpaWnQD\nnDWDUqq9vX3Hjh2PP/541Hd9LgH++c9/9vb2rlq16oEHHsjMzIzF39U2cYDm5mabzbZp0yaHw7Fk\nyZIY/WVx5/gb8O23305JSbntttviHOCSSy5Zv379mjVrfve7361evTovL+/GG2+Mc4aZM2e2t7eH\ntjVN6+joCC9jJw5HoaiYaoU0MjKSkJAQXhqNxuHh4Sm2x/PO4PV6H3jggYKCgjlz5sQ/wODg4Oef\nf97f39/b23vq1KnoBjhrhrGxsSeffLK4uDgpKSnquz6XAL29vXfddddrr722f//+mpqaV1999S9/\n+Us8AwQCgf3791ut1oMHD27fvv3tt9/evXt3dAOcNUNIMBisqKjweDxR3/tZA4yOjn7zzTe9vb3t\n7e1DQ0NtbW09PT1xznDXXXcdPXr0pZde+uyzz5599tlTp04NDQ1FPcMkNdUKyWQyRV4PGRwcNJlM\nU2yP55FheHh4y5Ytv/71r0tKSn71q1/FP4BSymKxbNq06f33329ubq6trY1zNVYPtQAAA8NJREFU\nhh07dpjN5ksvvbS5ufnkyZNdXV1R/1g6cYA5c+a8+OKLV111lVLqpptuWrZs2aeffhrPAElJSZdf\nfvmjjz5qMpnmzZt37733HjhwILoBzpohZP/+/cnJyQsXLoz63s8awOv17tmzZ/fu3c8999x77703\nMjLy1ltvxTnDVVdd9eabb37zzTcvvfTSdddd53a7r7jiiqhnmKQmxx+ueu5mzZoV+TcktbS0zJ07\nd4rt8TwyeDweo9G4Z8+eGTNmxD/A73//+66urqefflopNW3atPnz5x87dizOGQKBQEtLS1FRkVKq\no6MjMTFxeHi4uLg4bgEaGhoCgcCyZctCy7GxsajfSJs4QHp6emJiosFgCC0TExNjcSfvXH471NbW\n3nXXXVHf9bkE8Pl8aWlpKSkpSimDwWC323V5K46NjW3dujW0zM/Pv//++6OeYZKaamdImZmZQ0ND\nNTU1SqnGxsa6ujqXy6XLHqurq71eb0x3fY4ZPvroo7a2tvLy8hi10VkDzJ07d9euXY2NjUqp48eP\nf/zxx4sWLYpzhnXr1u3+3vLly1evXh3dNjprgGAwuHnz5ra2NqXUV199tX///ttvvz2eAZYuXTow\nMLB3716lVE9Pz65du3JycqIb4KwZQjOff/551O9inmOAjIyML7744tChQ0qpnp6evXv3xuLJjokz\nJCQkPPTQQ3//+9+VUp988onP5+OvMviB3o/5Rd/HH3/scDiWLl06f/78V155Ra89OhyOl156KTzz\nyiuvxPSBywkybNq06YYbbrgpwp/+9Kd4BtA07aWXXpo/f/5Pf/pTu91eXl4e9b2fS4aw3/72tzH6\n/5AmDlBeXr5w4cI77rhj6dKlb775ZvwDHDly5Gc/+1lOTs7ixYv/67/+a2RkJP4ZAoHA9ddfH9MH\nnScO8Prrr4f+b7AFCxZs2rRpdHQ0/hneeeedO++887bbbrv11ltDT4fHR6yPQhduav7hqpqmdXd3\nm81mozFO1yTjv0eBGSYOMDo62t3dfeWVV0be741zhjiYOMDIyMjJkyctFoteAZRSgUAgJSXlYv4p\njI2Nhb46ffp0vTIopU6ePHmOf/n1xWNqFhIAYNKZaveQAACTFIUEABCBQgIAiEAhAQBEoJAAACJQ\nSAAAESgkAIAIFBIAQAQKCQAgAoUEABCBQgIAiEAhAQBEoJAAACJQSAAAESgkAIAIFBIAQAQKCQAg\nAoUEABCBQgIAiEAhAQBEoJAAACJQSAAAESgkAIAIFBIAQAQKCQAgAoUEABDh/wBK5HQ3UswAywAA\nAABJRU5ErkJggg==\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "N = 10;\n",
    "x = linspace(0,1,N);\n",
    "plot(x,0,'b.','Markersize',16)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Generate A Matrix Equation\n",
    "\n",
    "Based on a grid, the function $u$ is discretized by a vector `u`. The\n",
    "derivative $u', u''$ are approximated by centeral finite difference:\n",
    "\n",
    "$$ u'(x_i) \\approx \\frac{u(i+1) - u(i-1)}{2h}$$\n",
    "\n",
    "$$ u''(x_i) \\approx \\frac{u(i-1) - 2u(i) + u(i+1)}{h^2}$$ \n",
    "\n",
    "The equation $-u''(x) = f(x)$ is discretized at $x_i, i=1,...,N$ as\n",
    "\n",
    "$$ \\frac{-u(i-1) + 2u(i) - u(i+1)}{h^2} \\quad = f(i),$$\n",
    "\n",
    "where $f(i) = f(x_i)$. These linear equations can be written as a matrix\n",
    "equation `A*u = f`, where `A` is a tri-diagonal matrix `(-1,2,-1)/h^2`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "ans =\n",
      "\n",
      "    -2     1     0     0     0\n",
      "     1    -2     1     0     0\n",
      "     0     1    -2     1     0\n",
      "     0     0     1    -2     1\n",
      "     0     0     0     1    -2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "n = 5;\n",
    "e = ones(n,1);\n",
    "A = spdiags([e -2*e e], -1:1, n, n);\n",
    "display(full(A));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use `spdiags` to speed up the generation of the matrix since the matrix is sparse. Compare `diag` and `spdiags`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stpe 3: Modify the Matrix Equation to Impose Boundary Conditions\n",
    "\n",
    "The discretization fails at boundary vertices since no nodes outside the\n",
    "interval. Howevery the boundary value is given by the problem: `u(1) = a,\n",
    "u(N) = b`.\n",
    "\n",
    "These two equations can be included in the matrix by changing `A(1,:) =\n",
    "[1, 0 ..., 0]` and `A(:,N) = [0, 0, ..., 1]` and `f(1) = a/h^2, f(N) =\n",
    "b/h^2`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "ans =\n",
      "\n",
      "     1     0     0     0     0\n",
      "     1    -2     1     0     0\n",
      "     0     1    -2     1     0\n",
      "     0     0     1    -2     1\n",
      "     0     0     0     0     1\n",
      "\n"
     ]
    }
   ],
   "source": [
    "A(1,1) = 1;\n",
    "A(1,2) = 0;\n",
    "A(n,n) = 1;\n",
    "A(n,n-1) = 0;\n",
    "display(full(A));"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Test the Correctness \n",
    "\n",
    "    [u,x] = poisson1D(0,1,5);\n",
    "    plot(x,sin(x),x,u,'r*');\n",
    "    legend('exact solution','approximate solution')\n",
    "\n",
    "Choose an exact solution $u=\\sin(x)$ and run your code to show the computed solution fits well on the curve of the true solution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Check the Convergence Rate\n",
    "\n",
    "    err = zeros(4,1);\n",
    "    h = zeros(4,1);\n",
    "    for k = 1:4\n",
    "        [u,x] = poisson1D(0,1,2^k+1);\n",
    "        uI = sin(x);\n",
    "        err(k) = max(abs(u-uI));\n",
    "        h(k) = 1/2^k;\n",
    "    end\n",
    "    display(err)\n",
    "    loglog(h,err,h,h.^2);\n",
    "    legend('error','h^2');\n",
    "    axis tight;\n",
    "    \n",
    "Change mesh size and show the convergence rate is second order, i.e. $h^2$.    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Matlab",
   "language": "matlab",
   "name": "matlab"
  },
  "language_info": {
   "codemirror_mode": "octave",
   "file_extension": ".m",
   "help_links": [
    {
     "text": "MetaKernel Magics",
     "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
    }
   ],
   "mimetype": "text/x-matlab",
   "name": "matlab",
   "version": "0.14.3"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "navigate_num": "#000000",
    "navigate_text": "#333333",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700",
    "sidebar_border": "#EEEEEE",
    "wrapper_background": "#FFFFFF"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "120px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
